﻿using System;
using System.Collections.Generic;

namespace Xom.Utilities
{
    /// <summary>
    /// Utitlities for Types
    /// </summary>
    public static class TypeUtility
    {
        /// <summary>
        /// Returns true if the given Type is one of the primitive types like string, int etc.
        /// </summary>
        /// <param name="type">Class Type</param>
        /// <returns></returns>
        public static bool IsSimpleType(Type type)
        {
            Type t = Nullable.GetUnderlyingType(type) ?? type;
            if (t.IsPrimitive || t.IsEnum || t == typeof(string)) return true;
            return false;
        }

        /// <summary>
        /// Returns true if the given Types is a IList Type.
        /// </summary>
        /// <param name="type">Class Type</param>
        /// <returns></returns>
        public static bool IsListType(Type type)
        {
            if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(IList<>)) return true;
            return false;
        }

        /// <summary>
        /// Returns the generic Type definition of the IList Type.
        /// </summary>
        /// <param name="type">Class Type</param>
        /// <returns></returns>
        public static Type GetListType(Type type)
        {
            //if (type != typeof(IList<>)) return null;
            return type.GetGenericArguments()[0];
        }
    }
}
